﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PCLawPrototype
{
    public partial class NewClientFrm : EditForm
    {
        private bool _isUpdate = false;
        private Client _editClient = null;
        private Contact _contact;
        private List<Lawyer> _lawyers = new List<Lawyer>();
        private List<Lawyer> _inChargeLawyersOld = new List<Lawyer>();
        private List<Lawyer> _inChargeLawyersNew = new List<Lawyer>();

        public NewClientFrm()
        {
            InitializeComponent();

            FillLawyerList(_inChargeLawyersOld);
        }

        public NewClientFrm(Client client)
        {
            InitializeComponent();
            _editClient = client;

            this.Text = "Edit Client [" + client.ClientCode + "]"; 
            txtFirmCode.Text = client.ClientCode;
            txtFirmName.Text = client.ClientName;

            client.LawyerInChargeOfClients.Load();
            foreach (LawyerInChargeOfClient lawyeric in client.LawyerInChargeOfClients)
            {
                lawyeric.LawyerReference.Load();

                FillLawyerToGrid(lawyeric.Lawyer);

                _inChargeLawyersOld.Add(lawyeric.Lawyer);
            }

            FillContactInfo(client.Contact);

            FillLawyerList(_inChargeLawyersOld);

            _isUpdate = true;
            _contact = client.Contact;
            btnOK.Text = "Save";
        }

        private void FillLawyerToGrid(Lawyer lawyer)
        {
            lawyer.LawyerRanks.Load();

            System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] {
            lawyer.LawyerName,
            lawyer.LawyerCode,
            lawyer.LawyerRanks.First(lawy => lawy.Status == 1).RankValue,
            lawyer.LawyerID}, -1);

            this.lstLawyer.Items.Add(listViewItem1);
        }

        private void FillLawyerList(List<Lawyer> inChargeLawyers)
        {
            //_lawyers = Common.Entity.Lawyers.TakeWhile(lawyer => !inChargeLawyers.Contains(lawyer)).ToList();
            _lawyers = Common.Entity.Lawyers.ToList();

            foreach (Lawyer lawyer in inChargeLawyers)
            {
                _lawyers.Remove(lawyer);
            }
            foreach (Lawyer lawyer in _lawyers)
            {
                cbxLawyer.Items.Add(lawyer.LawyerCode + " - " + lawyer.LawyerName);
            }
        }


        private void FillContactInfo(Contact contact)
        {
            txtFirstname.Text = contact.ContactFirstName;
            txtLastName.Text = contact.ContactLastName;
            txtMiddleName.Text = contact.ContactMiddleName;
            txtStreet.Text = contact.ContactAddress;
            txtCity.Text = contact.ContactCity;
            txtState.Text = contact.ContactState;
            txtZip.Text = contact.ContactZip;
            txtCountry.Text = contact.ContactCountry;
            txtAttention.Text = contact.ContactAttention;
            txtBusinessPhone.Text = contact.ContactBusinessPhone;
            txtHomePhone.Text = contact.ContactHomePhone;
            txtCellPhone.Text = contact.ContactCellPhone;
            txtOtherPhone.Text = contact.ContactOther;
            txtEmail1.Text = contact.ContactEmail1;
            txtEmail2.Text = contact.ContactEmail2;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Data has been saved!");

            if (_isUpdate)
            {
                Client cl = _editClient/*Common.Entity.Clients.First(client => client.ClientID == _editClient.ClientID)*/;
                cl.ContactReference.Load();
                cl.ClientName = txtFirmName.Text;
                cl.ClientCode = txtFirmCode.Text;

                if (!cl.Contact.ContactID.Equals(_contact.ContactID))
                {
                    cl.Contact = _contact;
                }

                foreach (Lawyer lawyer in _inChargeLawyersNew)
                {
                    cl.LawyerInChargeOfClients.Add(new LawyerInChargeOfClient()
                                                       {
                                                           Client = cl,
                                                           Lawyer = lawyer,
                                                           CreatedDate = DateTime.Now,
                                                           UpdatedDate = DateTime.Now
                                                       });
                }

                cl.UpdatedDate = DateTime.Now;
            }
            else
            {
                if (_contact != null)
                {
                    var client = new Client()
                                     {
                                         ClientID = Guid.NewGuid().ToString(),
                                         ClientCode = txtFirmCode.Text,
                                         ClientName = txtFirmName.Text,
                                         Contact = _contact,
                                         CreatedDate = DateTime.Now,
                                         UpdatedDate = DateTime.Now
                                     };
                    foreach (Lawyer lawyer in _inChargeLawyersNew)
                    {
                        client.LawyerInChargeOfClients.Add(new LawyerInChargeOfClient()
                                                               {
                                                                   Client = client,
                                                                   Lawyer = lawyer,
                                                                   CreatedDate = DateTime.Now,
                                                                   UpdatedDate = DateTime.Now
                                                               });
                    }
                    Common.Entity.AddToClients(client);
                }
            }
            Common.Entity.SaveChanges();
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnConflict_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Conflict of Information will be detected!");
        }

        private void btnAddToList_Click(object sender, EventArgs e)
        {
            if (cbxLawyer.SelectedIndex > -1)
            {
                _inChargeLawyersNew.Add(_lawyers[cbxLawyer.SelectedIndex]);
                FillLawyerToGrid(_lawyers[cbxLawyer.SelectedIndex]);
                _lawyers.RemoveAt(cbxLawyer.SelectedIndex);
                cbxLawyer.Items.RemoveAt(cbxLawyer.SelectedIndex);
                cbxLawyer.SelectedIndex = -1;
            }
        }

        private void btnSearchLawyer_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Lawyer's list filtered!");
        }

        private void btnPUContact_Click(object sender, EventArgs e)
        {
            PUContactFrm contactFrm = new PUContactFrm(this);
            contactFrm.ShowDialog();

            _contact = this.ManagedContact;
            FillContactInfo(_contact);
        }

        private void NewClientFrm_Load(object sender, EventArgs e)
        {
            //_lawyers = Common.Entity.Lawyers.ToList();
            //cbxLawyer.Items.AddRange(Common.Entity.Lawyers.Aggregate(new List(),(lst, i) => lst.Add(i.LawyerName + "(" + i.LawyerCode + ")")).ToArray());
        }
    }

    internal class List : List<string>
    {
        public List Add(string item)
        {
            base.Add(item);
            return this;
        }
    }
}
